package com.javasm.mingming.login.authorize;

import com.javasm.mingming.adminuser.entity.AdminAuthority;
import com.javasm.mingming.adminuser.entity.AdminUser;
import com.javasm.mingming.login.entity.LoginUserDetails;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author javasm
 * @version 0.1
 * @className JavasmPreAuthorize
 * @descriptioin:
 * @date 2025/1/15 14:47
 * @since jdk11
 */
@Component
public class JavasmPreAuthorize {

    public boolean check(String code) {
        //获取登录用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal != null) {
            LoginUserDetails userDetails = (LoginUserDetails) principal;
            //用户自己的 用户信息
            AdminUser adminUser = userDetails.getAdminUser();
            //获取权限列表
            List<AdminAuthority> authorityList = adminUser.getAuthorityList();
            //把 权限列表中,所有的code 都集合到一起
            if (authorityList != null && authorityList.size() > 0){
                //List<code>
                List<String> codeList = authorityList.stream().map(AdminAuthority::getCode).collect(Collectors.toList());
                //是否包含字符串
                return codeList.contains(code);
            }

        }
        return false;
    }
}
